package com.store.controller.hzl;

import com.store.entity.*;
import com.store.service.hzl.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * ClassName:indexOperatorsController
 * package:com.store.controller.hzl
 * Description:
 *
 * @Date:2021/1/17 14:18
 * @Author: tank_黄志凌
 */
@Controller
@RequestMapping("/index_hzl")
public class IndexOperatorsController {

    @Autowired
    private IndexService indexService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private SellerService sellerService;

    @Autowired
    private SpecificationService specificationService;

    @Autowired
    private TypeTemplateService typeTemplateService;

    @Autowired
    private ContentCategoryService contentCategoryService;

    @Autowired
    private ItemCatService itemCatService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ContentService contentService;

    @RequestMapping("/toIndexOperators")
    public String toIndexOperators(){
        return "Operators/indexOperators";
    }

    @RequestMapping("/toHome")
    public String toHome(){
        return "Operators/home";
    }

    @RequestMapping("/toSeller")
    public String toSeller(Model model,PageManager pm){
        List<Seller> sellerList=indexService.querySellers(pm);

        int totalCount=sellerService.querySellerTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("sellers",sellerList);
        model.addAttribute("pm",pm);
        return "Operators/seller";
    }


    @RequestMapping("/toBrand")
    public String toBrand(Model model,PageManager pm){
        List<Brand> brands=indexService.queryBrand(pm);

        int totalCount=brandService.queryBrandTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("brands",brands);
        model.addAttribute("pm",pm);
        return "Operators/brand";
    }

    @RequestMapping("/toSpecification")
    public String toSpecification(Model model,PageManager pm){
        List<Specification> specifications=indexService.querySpecification(pm);

        int totalCount=specificationService.querySpecificationTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("specifications",specifications);
        model.addAttribute("pm",pm);
        return "Operators/specification";
    }

    @RequestMapping("/toTypeTemplate")
    public String toTypeTemplate(Model model,PageManager pm){
        List<TypeTemplate> typeTemplates=indexService.queryTypeTemplate(pm);

        int totalCount=typeTemplateService.queryTypeTemplateTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("typeTemplates",typeTemplates);
        model.addAttribute("pm",pm);
        return "Operators/typeTemplate";
    }

    @RequestMapping("/toContentCategory")
    public String toContentCategory(Model model,PageManager pm){
        List<ContentCategory> contentCategories=indexService.queryContentCategory(pm);

        int totalCount=contentCategoryService.queryTypeTemplateTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("contentCategories",contentCategories);
        model.addAttribute("pm",pm);
        return "Operators/content_category";
    }

    @RequestMapping("/toContent")
    public String toContent(Model model,PageManager pm){
        List<Content> contents=indexService.queryContent(pm);

        int totalCount=contentService.queryContentTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("contents",contents);
        model.addAttribute("pm",pm);
        return "Operators/content";
    }

    @RequestMapping("/toGoods")
    public String toGoods(Model model,PageManager pm){
        List<Goods> goods=indexService.queryGoods(pm);

        int totalCount=goodsService.queryGoodsTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("goods",goods);
        model.addAttribute("pm",pm);
        return "Operators/goods";
    }

    @RequestMapping("/toItemCat")
    public String toItemCat(Model model,PageManager pm){
        List<ItemCat> itemCats=indexService.queryItemCat(pm);

        int totalCount=itemCatService.queryItemCatTotalCount();
        pm.setTotalCount(totalCount);
        pm.setPages();

        model.addAttribute("itemCats",itemCats);
        model.addAttribute("pm",pm);
        return "Operators/item_cat";
    }

    @RequestMapping("/showPic")
    public void showPic(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String pic=request.getParameter("pic");
        File file = new File(pic);
        //将图片的字节写入到页面
        //jsp中的out写入东西
        OutputStream out = response.getOutputStream();
        InputStream in = new BufferedInputStream(new FileInputStream(file));
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        out.close();
        in.close();
        response.flushBuffer();//刷新缓存
    }
}
